package PJ;

import java.util.ArrayList;
import java.util.Scanner;

import place.Landindex;
import card.CardSystem;

public class Player {
	private double deposit ;
	private double cash;
	private int Point;
	private int housequantity;
	private int step;
	private double houseValue;
	private int direction =1;
	private int location=(int)(Math.random()*67+1);
	private String name ;
	private int namei;
	private String direct ;
	private int special=0;//=0时正常，=1-6时使用了遥控骰子，=7时处于滞留卡状态，=8时处于乌龟卡状态，=9-16时处于福神保佑状态，=17-24时处于财神保佑状态
	private int[] cards=new int[17];
	private int[] stocks=new int[10];
	Player(double deposit,double cash,int point,int housequantity,int housevalue,int direction,String name,int namei,int special){
		this.deposit=deposit;
		this.cash=cash;
		this.Point=point;
		this.housequantity=housequantity;
		this.houseValue=housevalue;
		this.direction=direction;
		this.name=name;
		this.special=special;
		this.namei=namei;
		for(int i=0;i<=16;i++){
			cards[i]=1;
		}
	}
	public Player() {
		// TODO Auto-generated constructor stub
	}
	public void move(){
		if (deposit + cash > 0){
		if(direction==1){
			direct="顺时针";
		}else{
			direct="逆时针";
		}
		System.out.println("现在是玩家"+name+"的回合，您的前进方向是"+direct+"。");
		if(Date.day==1){
			System.out.println("又到了月初，您的存款已在月末获得了10%的利息");
			this.deposit=deposit*1.1;
		}
		System.out.println("您现在可以进行如下操作：");
		MenuPrint.print1();
		Scanner input = new Scanner(System.in);
		int digitalInput = input.nextInt();
		while (true) {
			if (digitalInput >= 0 && digitalInput <= 8)
				break;
			else {
				System.out.print("invalid input, please enter again :");
				digitalInput = input.nextInt();
			}
		}
		while (digitalInput != 6 && digitalInput != 7) {
			switch (digitalInput) {
			case 0:
				MenuPrint.printmap();
				System.out.println("还有什么要看的");
				MenuPrint.print1();
				digitalInput = input.nextInt();	
				break;
			case 1:
				MenuPrint.printinitialmap();
				System.out.println("还有什么要看的");
				MenuPrint.print1();
				digitalInput = input.nextInt();						
				break;
			case 2:
				System.out.println("名称"+"\t\t\t\t"+"功能"+"\t\t"+"类型"+"\t\t"+"价格"+"\t\t"+"适用范围"+"\t\t"+"剩余数量");
				for(int i=0;i<=16;i++){
					System.out.println(i+":"+CardSystem.cardsystem[i].getName()+"\t\t"+CardSystem.cardsystem[i].getFunction()+"\t\t"+CardSystem.cardsystem[i].getType()+"\t\t"+CardSystem.cardsystem[i].getPoint()+"\t\t"+CardSystem.cardsystem[i].getRange()+"\t\t"+cards[i]);
				}
				System.out.println("请选择");
				Scanner in=new Scanner(System.in);
				int inner=in.nextInt();
				if(cards[inner]==0){
					System.out.println("此卡数量不足");
				}else{
					CardSystem.cardsystem[inner].print(location, namei);						
					cards[inner]--;
				}
				System.out.println("还有什么要看的");
				MenuPrint.print1();
				digitalInput = input.nextInt();					
				break;
			case 3:
				boolean luzhang=false;
				for (int i = 1; i <= 10; i++) {
					if (Landindex.mark[location+i].state == false) {
						System.out.println("前方第" + i + "步处有路障，小心哦,还有什么要看的");
						luzhang=true;
					}
				}
				if(!luzhang){
					System.out.println("没有情况；还有什么要看的");
				}
				MenuPrint.print1();
				digitalInput = input.nextInt();					
				break;
			case 4:
				System.out.print("请输入您想查询的地方与您相差的步数<后方用负数，按q退出>:");
				String Input = input.next();
				if (Input.equals("q")) {
					System.out.println("还有其它想看的么，请输入 ：");
					MenuPrint.print1();
					digitalInput = input.nextInt();
					break;
				} else {
					int i = Integer.parseInt(Input);
					Landindex.mark[(location+i)%68].printInformation((location+i)%68);
					System.out.println("还有其它想的么，请输入 ：");
					MenuPrint.print1();
					digitalInput = input.nextInt();
					break;
				}
			case 5:
				System.out.println("玩家"+"\t"+"现金    "+"\t"+"存款     "+"\t"+"房产数"+"\t"+"房产价值"+"\t"+"点券数");
				for(int i=0;i<Entrance.players.size();i++){
					System.out.println(Entrance.players.get(i).getName()+"\t"+Entrance.players.get(i).getCash()+"\t"+Entrance.players.get(i).getDeposit()+"\t"+Entrance.players.get(i).getHousequantity()+"\t"+Entrance.players.get(i).getHouseValue()+"\t"+Entrance.players.get(i).getPoint());					
				}
				System.out.println("还有其它想的么，请输入 ：");
				MenuPrint.print1();
				digitalInput = input.nextInt();
				break;	
			case 8:
					System.out.println("欢迎进入大富翁股市~~");
					MenuPrint.printstock(namei);
					String[] stockinput;
					String choose;
					int num;
					int amount;
					System.out.println("买股票请输入b*序号*数量，卖股票请输入s*序号*数量,退出请输入q");
					String stockin=input.next();
					if(stockin.equals("q")){
						System.out.println("还有其它想的么，请输入 ：");
						MenuPrint.print1();
						digitalInput = input.nextInt();
						break;
					}else{
					stockinput=stockin.split("[*]");
					choose=stockinput[0];
					num=Integer.parseInt(stockinput[1]);
					amount=Integer.parseInt(stockinput[2]);
					while(true){
					if(choose.equals("b")){
						double allprice=Stock.stocksystem[num-1].getPrice()*amount;
						double all=deposit+cash;
						if(all>=allprice){
							if(deposit>=allprice){
							deposit-=allprice;
							}else{
								cash-=(allprice-deposit);
								deposit=0;
							}
							stocks[num-1]+=amount;
							Stock.stocksystem[num-1].setHold(Stock.stocksystem[num-1].getHold()+amount);
							System.out.println("购买成功!");
							break;
						}else{
							System.out.println("购买失败，您的金钱不足");
							break;
						}		
					}else if(choose.equals("s")){
						//System.out.println("您当前持有量为"+stocks[num-1]+"您要卖出"+amount);
						if(stocks[num-1]>=amount){
						double allprice=Stock.stocksystem[num-1].getPrice()*amount;
						stocks[num-1]-=amount;
						deposit+=allprice;
						Stock.stocksystem[num-1].setHold(Stock.stocksystem[num-1].getHold()-amount);
						System.out.println("抛售成功！");
						break;
						}else{
							System.out.println("抛售失败，您的持有量不足");
							break;
						}
					}else{
					System.out.print("invalid input, please enter again :");
					stockin=input.next();
					}
					}	
					System.out.println("还有其它想的么，请输入 ：");
					MenuPrint.print1();
					digitalInput = input.nextInt();
				break;
				}
			}
		}
		if (digitalInput == 6) {
			int actualStep = (int)(Math.random()*6+1);
			//System.out.println("前面"+actualStep);
			//当使用滞留卡时
			if(special==7){
				actualStep=0;
				special=0;
			//使用遥控骰子时
			}else if(special>0&&special<=6){
				actualStep=special;
				special=0;
			}			
			//遇到路障时
			for(int i= 1; i < actualStep; i++){
				if (Landindex.mark[(location+i* direction)%68].state == false) {
					System.out.println("前面第" + i + "步遇到了路障，过不去了");
					actualStep = i;
					Landindex.mark[(location+i* direction)%68].state=true;
					location=(location+i* direction)%68;
					break;
				}	
			}
			//当经过银行时
			for (int j = 1; j < actualStep; j++) {
				if (Landindex.mark[(location+j* direction)%68] instanceof place.Bank) {
					Landindex.mark[(location+j* direction)%68].printaction((location+j* direction)%68, namei);
					break;
				}
			}							
			location=(location+actualStep*direction)%68;
		//	System.out.println("后面"+actualStep);
			Landindex.mark[location].printaction(location, namei);
		}			
		if (digitalInput == 7) {
			System.out.println("祝您下次游戏愉快~~");
			for(int i=0;i<Entrance.players.size();i++){
				if(Entrance.players.get(i).getNamei()==namei){
					Entrance.players.remove(i);
				}
			}
			for(int m=0;m<=67;m++){
				if(Landindex.mark[m].owner==namei){
					Landindex.mark[m].owner=0;								
				}
			}
			if(Entrance.players.size()==1){
				System.out.println("胜利的玩家是:"+Entrance.players.get(0).getName());
				System.exit(0);
			}
		}		
		}else{
		System.out.println("真抱歉，您破产了！下次加油吧！");	
		for(int i=0;i<Entrance.players.size();i++){
			if(Entrance.players.get(i).getNamei()==namei){
				Entrance.players.remove(i);
			}
		}
		}
	}
	public double getDeposit() {
		return deposit;
	}
	public void setDeposit(double deposit) {
		this.deposit = deposit;
	}
	public double getCash() {
		return cash;
	}
	public void setCash(double cash) {
		this.cash = cash;
	}
	public int getPoint() {
		return Point;
	}
	public void setPoint(int point) {
		Point = point;
	}
	public int getHousequantity() {
		return housequantity;
	}
	public void setHousequantity(int housequantity) {
		this.housequantity = housequantity;
	}
	public int getStep() {
		return step;
	}
	public void setStep(int step) {
		this.step = step;
	}
	public double getHouseValue() {
		return houseValue;
	}
	public void setHouseValue(double d) {
		this.houseValue = d;
	}
	public int getDirection() {
		return direction;
	}
	public void setDirection(int direction) {
		this.direction = direction;
	}
	public int getLocation() {
		return location;
	}
	public void setLocation(int location) {
		this.location = location;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNamei() {
		return namei;
	}
	public void setNamei(int namei) {
		this.namei = namei;
	}
	public int getSpecial() {
		return special;
	}
	public void setSpecial(int special) {
		this.special = special;
	}
	public int[] getCards() {
		return cards;
	}
	public void setCards(int[] cards) {
		cards = cards;
	}
	public int[] getStocks() {
		return stocks;
	}
	public void setStocks(int[] stocks) {
		this.stocks = stocks;
	}
}
